/**
 * Test how fast travel a array by index and pointer.
 *Author:zt Date:2013/10/14
 */
#include<stdio.h>
#include<stdlib.h>
#include<time.h>



void travelByIndex(char* array, int size)
{
	clock_t eclapse = clock();
	int i;
	char j;
	for(i = 0, j = 1; i < size; i++, j = (j++)%255)
	{
		array[i] = j;
		array[i] = j;
		array[i] = j;
		array[i] = j;
		array[i] = j;
		array[i] = j;
		array[i] = j;
		array[i] = j;
		array[i] = j;
		array[i] = j;
	}
	eclapse = clock() - eclapse;
	printf("Travel by index: %4dms\n", eclapse);
	return ;
}

void travelByPointer(char* array, int size)
{
	char* p = array;
	clock_t eclapse = clock();
	char j;
	for(j = 1; p != &array[size]; p++, j = (j++)%255)
	{
		*p = j;
		*p = j;
		*p = j;
		*p = j;
		*p = j;
		*p = j;
		*p = j;
		*p = j;
		*p = j;
		*p = j;
	}
	eclapse = clock() - eclapse;
	printf("Travel by pointer: %dms\n", eclapse);
	return ;
}

int main(void)
{
	int size = 1000000;
	char* array = malloc(sizeof(char) * size);
	travelByIndex(array, size);
	travelByPointer(array, size);
	getchar();
	return 0;
}